//
//  MYCIndexCardViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/22.
//  Copyright © 2017年 hbjApple. All rights reserved.
//我的资料卡

#import "MYCIndexCardViewController.h"
#import "MYXRedListHeadCell.h"
#import "MYXRedListCell.h"
#import "MYCIndexCardCell.h"
#import "MYXotheruserinfoModel.h"
#import "myfollowListModel.h"
static NSString *const MYXRedListHeadCellIdentifier = @"MYXRedListHeadCell";
static NSString *const MYCIndexCardCellIdentifier = @"MYCIndexCardCell";
@interface MYCIndexCardViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>{
    CGFloat lastOriginalY;
    MYXRedListHeadCell *MYXRedListHeadCellId ;
    MYCIndexCardCell *MYCIndexCardCe ;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)MYXotheruserinfoModel *MYXotheruserinfoM;
@end

@implementation MYCIndexCardViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initIndexCardRequest:self.userID];
    [self createUI];
}

#pragma mark - UI

- (void)createUI
{
    
    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(-SafeAreaTopHeight));
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tableView registerNib:[UINib nibWithNibName:MYXRedListHeadCellIdentifier bundle:nil] forCellReuseIdentifier:MYXRedListHeadCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:MYCIndexCardCellIdentifier bundle:nil] forCellReuseIdentifier:MYCIndexCardCellIdentifier];
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -SafeAreaTopHeight, WIDTH, HEIGHT+20) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        //        WS(weakSelf)
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            [weakSelf loadNewData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [weakSelf loadMorewData];
        //        }];
        
    }
    return _tableView;
}
#pragma mark - request

- (void)loadNewData
{
    [self.tableView.mj_header beginRefreshing];
    [self.dataArr removeAllObjects];
    
    //  [self initUsefrozenmoney:self.pageStart];
    
}
- (void)loadMorewData
{
    if (self.tableView.mj_header.isRefreshing) {
        return;
    }
    
    // [self initUsefrozenmoney:self.pageStart ];
}
#pragma mark - tableview datasource and delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return 4;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    if (indexPath.section==0) {
        MYXRedListHeadCellId = [tableView dequeueReusableCellWithIdentifier:MYXRedListHeadCellIdentifier forIndexPath:indexPath];
        MYXRedListHeadCellId.selectionStyle = UITableViewCellSelectionStyleNone;
        MYXRedListHeadCellId.MYXotheruserinfoM =self.MYXotheruserinfoM;
        MYXRedListHeadCellId.fllowB =^{
            if ([weakSelf.MYXotheruserinfoM.is_follow isEqualToString:@"1"]) {
                [weakSelf initcanfollow:weakSelf.MYXotheruserinfoM.userID];
            }else{
                [weakSelf initAddfollow:weakSelf.MYXotheruserinfoM.userID];
            }
        };
        return MYXRedListHeadCellId;
    }
    MYCIndexCardCe = [tableView dequeueReusableCellWithIdentifier:MYCIndexCardCellIdentifier forIndexPath:indexPath];
    MYCIndexCardCe.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row==0) {
        MYCIndexCardCe.homeLabel.text =@"家乡";
        MYCIndexCardCe.titileLabel.text = self.MYXotheruserinfoM.home;
    }else if (indexPath.row==1){
        MYCIndexCardCe.homeLabel.text =@"生日";
        MYCIndexCardCe.titileLabel.text =  [MYXDateTimeHelper birthDayTimeWithIntervalString:self.MYXotheruserinfoM.birthday];
    }else if (indexPath.row==2){
        MYCIndexCardCe.homeLabel.text =@"签名";
        MYCIndexCardCe.titileLabel.text = self.MYXotheruserinfoM.signature;
    }else if (indexPath.row==3){
        MYCIndexCardCe.homeLabel.text =@"标签";
        MYCIndexCardCe.titileLabel.text = self.MYXotheruserinfoM.tags;
    }
    return MYCIndexCardCe;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 360;
    }
    return 51;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"scrollview offset %f",scrollView.contentOffset.y);
    lastOriginalY = scrollView.contentOffset.y;
    CGFloat consTop;
    if (lastOriginalY < -20) {
        consTop  = - 20 - lastOriginalY;
        
    }else{
        consTop = 0;
    }
    
    CGFloat alpha = (consTop+360)/360;
    alpha=fabs(alpha);
    alpha=fabs(1-alpha);
    
    alpha=alpha<0.2? 0:alpha-0.2;
    if (MYXRedListHeadCellId) {
        MYXRedListHeadCellId.bg_conTop.constant = -consTop;
        //  MYXRedListHeadCellId.bgImageView.alpha =alpha;
    }
    
}

-(void)initIndexCardRequest:(NSString *)userID{
    [SVProgressHUD show];
    self.MYXotheruserinfoM =[[MYXotheruserinfoModel alloc]init];
    [[MYXHttpManager shared]otheruserinfo:userID withResult:^(BOOL success, NSString *msg, id object) {
        if (object) {
            [SVProgressHUD dismiss];
            self.MYXotheruserinfoM= object;
            [self.tableView reloadData];
        }
        
    } errorResult:^(NSError *enginerError) {
        [SVProgressHUD dismiss];
        // [JXTHUDHelper showHUDText:enginerError type:@"error" inView:nil];
    }];
}

#pragma mark - 加关注


-(void)initAddfollow:(NSString *)userID{
    
    [[MYXHttpManager shared] addfollow:userID success:^(NSString *dataStr) {
        [JXTHUDHelper showHUDText:@"成功加入关注" type:@"success" inView:nil];
        [self initIndexCardRequest:self.userID];
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
        [JXTHUDHelper showHUDText:@"网络出错！" type:@"error" inView:nil];
    }];
    
}


#pragma mark -  取消关注


-(void)initcanfollow:(NSString *)userID{
    
    [[MYXHttpManager shared] cancelfollow:userID success:^(NSString *dataStr) {
        [JXTHUDHelper showHUDText:@"成功取消关注" type:@"success" inView:nil];
        [self initIndexCardRequest:self.userID];
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
        [JXTHUDHelper showHUDText:@"网络出错！" type:@"error" inView:nil];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

